
package com.yimixian.app.ui;

import com.yimixian.app.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import butterknife.ButterKnife;

public class ParabolaView extends FrameLayout {

    private int mCurrentTime = 0;
    private int mTargetOffSetOfX;
    private int mTargetOffSetOfY;
    private int mStartOffSetOfX;
    private int mStartOffSetOfY;
    private float mAssistTopValue;
    private float mAssistBottomValue;
    private int mTargetX;
    private int mTargetY;
    private int mStartX;
    private int mStartY;
    private float mAnimationRatio;
    private int mTimeParameter;
    private float mTimeRatio;
    private int mTotalTime;
    private Bitmap animBack;
    private ImageView iv_main;

    public ParabolaView(Context context) {
        super(context);
        initParabolaView();
    }

    public ParabolaView(Context context, Bitmap animBack) {
        super(context);
        this.animBack = animBack;
        initParabolaView();
    }

    public ParabolaView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initParabolaView();
    }

    public ParabolaView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParabolaView();
    }

    private void initParabolaView() {
        LayoutInflater.from(getContext()).inflate(R.layout.parabola_view, this);
        iv_main = (ImageView) findViewById(R.id.iv_main);
        if(animBack != null){
            iv_main.setImageBitmap(animBack);
        }
        this.setVisibility(View.GONE);
        ButterKnife.inject(this);
        mTargetOffSetOfX = (int) getContext().getResources().getDimension(
                R.dimen.parabola_animation_target_offset_x);
        mTargetOffSetOfY = (int) getContext().getResources().getDimension(
                R.dimen.parabola_animation_target_offset_y);
        mStartOffSetOfX = (int) getContext().getResources().getDimension(
                R.dimen.parabola_animation_start_offset_x);
        mStartOffSetOfY = (int) getContext().getResources().getDimension(
                R.dimen.parabola_animation_start_offset_y);

        mAssistTopValue = getContext().getResources().getDimension(
                R.dimen.parabola_animation_assist_top_value);
        mAssistBottomValue = getContext().getResources().getDimension(
                R.dimen.parabola_animation_assist_bottom_value);
    }

    public void startAnimation(final Handler handler, Point targetPoint, Point startPoint) {

        mTargetX = targetPoint.x + mTargetOffSetOfX;
        mTargetY = targetPoint.y + mTargetOffSetOfY;
        mStartX = startPoint.x + mStartOffSetOfX;
        mStartY = startPoint.y + mStartOffSetOfY;

        // 简化处理
        if (mStartY > mTargetY - mAssistBottomValue) {
            mStartY = mTargetY - (int) mAssistBottomValue;
        }

        // 到达顶点 消耗的时间比例 和 动画总时间 修正
        if (mStartY < mTargetY - mAssistTopValue) {
            mTimeRatio = 0.1f;
            mTotalTime = 600;
        } else if (mStartY == mTargetY - mAssistBottomValue) {
            mTimeRatio = 0.48f;
            mTotalTime = 375;
        } else {
            mTimeRatio = ((mStartY - mTargetY) / mAssistTopValue * (0.38f)) + 0.48f;
            mTotalTime = (int) ((mStartY - mTargetY) / mAssistTopValue * (-225)) + 375;
        }

        mTimeParameter = (int) (mTotalTime * mTimeRatio);

        float parameter = ((mTotalTime - mTimeParameter) * (mTotalTime - mTimeParameter) - mTimeParameter
                * mTimeParameter);
        mAnimationRatio = (mStartY - mTargetY) / parameter;
        final long initTime = System.currentTimeMillis();
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                ParabolaView.this.setVisibility(View.VISIBLE);
                mCurrentTime = (int) (System.currentTimeMillis() - initTime);
                if (mCurrentTime < mTotalTime + 1) {
                    int paddingLeft = mStartX + (mTargetX - mStartX) * mCurrentTime / mTotalTime;
                    int paddingTop = (int) (mAnimationRatio * ((mTotalTime - mTimeParameter)
                            * (mTotalTime - mTimeParameter) -
                            (mCurrentTime - mTimeParameter) * (mCurrentTime - mTimeParameter)))
                            + mTargetY;
                    setTranslationX(paddingLeft);
                    setTranslationY(paddingTop);
                    handler.post(this);
                } else {
                    if(null!=animListener)animListener.onEndAnim();
                    handler.removeCallbacks(this);
                    ViewGroup mParent = (ViewGroup) ParabolaView.this.getParent();
                    if (mParent != null) {
                        mParent.removeView(ParabolaView.this);
                    }
                }
            }
        };
        handler.post(runnable);
    }
    public interface AnimListener{
        void onEndAnim();
    }
    private AnimListener animListener;
    public void setAnimListener(AnimListener listener){
        this.animListener = listener;
    }
}
